using System;
using System.Threading;
using System.Threading.Tasks;
using Agriculture.Api.Read.Application.Command;
using Dapper;
using MediatR;
using Microsoft.Extensions.Configuration;
using MySql.Data.MySqlClient;

namespace Agriculture.Api.Read.Application.Handler
{
    /// <summary>
    /// 挂号订单详情查询处理器
    /// </summary>
    public class RegistrationOrderDetailQueryHandler : IRequestHandler<RegistrationOrderDetailQuery, RegistrationOrderDetailResult>
    {
        private readonly IConfiguration _configuration;

        public RegistrationOrderDetailQueryHandler(IConfiguration configuration)
        {
            _configuration = configuration;
        }

        public async Task<RegistrationOrderDetailResult> Handle(RegistrationOrderDetailQuery request, CancellationToken cancellationToken)
        {
            string connectionString = _configuration.GetConnectionString("DefaultConnection");

            using (var connection = new MySqlConnection(connectionString))
            {
                // 查询订单基本信息、挂号信息和就诊人信息，不使用JOIN，使用单表查询
                var orderSql = @"
                    SELECT 
                        ro.AppointmentTime, ro.Hospital, ro.Department, ro.DoctorName AS Doctor, 
                        ro.DoctorTitle, ro.ServiceFee AS RegistrationFee, ro.RegistrationNo,
                        
                        ro.OrderNo, ro.Status, ro.OrderAmount AS PayableAmount, ro.DiscountAmount AS CouponAmount, ro.PayAmount AS ActualPayment,
                        ro.CreateTime, ro.PayMethod AS PaymentMethod, ro.PayTime AS PaymentTime, 0 AS RefundAmount, NULL AS RefundTime,
                        
                        ro.PatientName AS Name, ro.PatientGender AS Gender, ro.PatientAge AS Age, ro.PatientPhone AS Phone
                    FROM registrationordermodel ro
                    WHERE ro.OrderNo = @OrderNo AND ro.IsDeleted = 0";

                var orderDetail = await connection.QueryFirstOrDefaultAsync<dynamic>(orderSql, new { OrderNo = request.OrderNo });

                if (orderDetail == null)
                {
                    return null;
                }

                var result = new RegistrationOrderDetailResult
                {
                    RegistrationInfo = new RegistrationInfoDto
                    {
                        AppointmentTime = orderDetail.AppointmentTime,
                        Hospital = orderDetail.Hospital,
                        Department = orderDetail.Department,
                        Doctor = orderDetail.Doctor,
                        DoctorTitle = orderDetail.DoctorTitle,
                        RegistrationFee = orderDetail.RegistrationFee,
                        RegistrationNo = orderDetail.RegistrationNo
                    },
                    OrderInfo = new OrderInfoDto
                    {
                        OrderNo = orderDetail.OrderNo,
                        Status = ConvertOrderStatus(orderDetail.Status),
                        PayableAmount = orderDetail.PayableAmount,
                        CouponAmount = orderDetail.CouponAmount,
                        ActualPayment = orderDetail.ActualPayment,
                        CreateTime = orderDetail.CreateTime,
                        PaymentMethod = orderDetail.PaymentMethod,
                        PaymentTime = orderDetail.PaymentTime,
                        RefundAmount = orderDetail.RefundAmount,
                        RefundTime = orderDetail.RefundTime
                    },
                    PatientInfo = new RegistrationPatientInfoDto
                    {
                        Name = orderDetail.Name,
                        Gender = orderDetail.Gender,
                        Age = orderDetail.Age,
                        Phone = orderDetail.Phone
                    }
                };

                return result;
            }
        }

        /// <summary>
        /// 转换订单状态码为状态文本
        /// </summary>
        private string ConvertOrderStatus(int status)
        {
            switch (status)
            {
                case 1:
                    return "待支付";
                case 2:
                    return "已完成";
                case 3:
                    return "已取消";
                case 4:
                    return "已退款";
                default:
                    return "未知";
            }
        }
    }
} 